<template>
	<view class="box_conten">
		<view class="main_bg">
			<scroll-view :refresher-triggered="triggered" refresher-background="transparent"
				@refresherpulling="refresherpulling" refresher-enabled="true" scroll-y="true"
				@scrolltolower="scrolltolower" class="scroll_y" @refresherrefresh="onRefresh">
				<view v-if="goodsList.length>0" class="back_mag">
					<view v-for="(item,index) in goodsList" :key="index" class="item_li">
						<view class="conent" @click="goDetail(item.id)">
							<view class="left_img">
								<image v-if="item.image" @click="lookImg(item.image)" class="img" lazy-load
									:src="item.image" mode="aspectFill"></image>
								<uni-icons color="#aac" size="48" v-else type="image"></uni-icons>
							</view>
							<view class="right_text">
								<view class="brands">
									{{item.goodsName}}
								</view>
								<view class="top_brand_type">
									<span class="tag" v-if="!item.overInsured">在保</span>
									<span class="tag tag_p" v-else>已过保</span>
									<span class="day_color" v-if="!item.overInsured">质保周期:{{item.warrantyDay}}天</span>
									<a class="bule" @click.stop="goPages(item.id)">查看保修卡</a>
								</view>
								<view class="pt"><span class="tip">型号：</span><span
										class="lis_con">{{item.goodsModel}}</span></view>
								<view class="pt"><span class="tip">规格：</span><span
										class="lis_con">{{item.goodsSpecName}}</span></view>

								<view class="pt"><span class="tip">SN码/一物一码：</span><span
										class="lis_con">{{item.code}}</span></view>
								<view class="pt"><span
										class="tip">质保时间：</span>{{item.warrantyStartTime.split(' ')[0]+' ~ '+item.warrantyEndTime.split(' ')[0]}}
								</view>
							</view>
						</view>
					</view>
					<view class="no_some" v-if="title">
						{{title}}
					</view>
				</view>
				<view v-else class="void_goods">
					<image class="img" src="/static/goods.svg" mode="aspectFit"></image>
					<view class="tip">暂无产品</view>
				</view>
			</scroll-view>
		</view>
		<view class="btn_logout">
			<button type="primary" size="small" @click="addOpen" class="onlogout">
				<span>激活保修/登记产品</span>
			</button>
		</view>
		<!-- 添加产品 弹窗 -->
		<uni-popup ref="popup" type="center">
			<view class="add_main">
				<p>激活保修</p><uni-icons class="close_this" size="20" @click="popup.close()" color="#999"
					type="closeempty"></uni-icons>
				<uni-easyinput suffixIcon="scan" placeholderStyle="font-size:28rpx;" v-model="code"
					placeholder="请输入或扫描机身编码" @iconClick="iconClick"></uni-easyinput>
				<button class="onlogout" @click="addCode"><span>添加</span></button>
			</view>
		</uni-popup>
	</view>
</template>

<script setup lang="ts">
	import {
		ref, onMounted
	} from "vue";
	import { onUnload } from '@dcloudio/uni-app'
	import apiGoods from '@/util/api/goods.js'
	import apiCode from '@/util/api/code.js'
	const goodsList = ref([])
	const total = ref(0)
	const page = ref(1)
	const popup = ref()
	const code = ref('')
	const title = ref('没有更多产品了')
	const addOpen = () => {
		//打开弹窗
		popup.value.open()
	}
	onMounted(() => {
		getList()
	})
	// 下拉刷新触发
	const triggered = ref(false)
	const refresherpulling = (e) => {
		if (e.detail.deltaY < 0) return // 防止上滑页面也触发下拉
		triggered.value = true
	}
	const onRefresh = async () => {
		goodsList.value = []
		page.value = 1
		code.value = ''
		await getList()
		uni.stopPullDownRefresh(); // 停止下拉刷新状态
		setTimeout(() => {
			triggered.value = false
		}, 500)
	}
	// 滚到底部触发
	const scrolltolower = () => {
		if (total.value > goodsList.value.length) {
			page.value = page.value + 1
			title.value = ''
			getList()
		} else {
			title.value = '没有更多产品了'
		}
	}
	// 电子保修卡信息
	const goPages = (id : string) => {
		uni.navigateTo({
			url: '/pages_user/card/detail?id=' + id
		})
	}
	onUnload(() => {
		uni.hideLoading()
	})
	// 获取产品列表
	const getList = async () => {
		let data = {
			"limit": 0,
			"page": page.value
		}
		uni.showLoading({
			title: '加载中...'
		})
		await apiGoods.getGoodsList(data).then((res : { code : number; data : any; }) => {
			if (res.code === 20000) {
				goodsList.value = [...goodsList.value, ...res.data.rows]
				total.value = res.data.total
			}
		})
		uni.hideLoading()
	}
	// 产品详情
	const goDetail = (id : string) => {
		uni.navigateTo({
			url: '/pages_user/goods/detail?id=' + id
		})
	}
	// 预览图片
	const lookImg = (url) => {
		uni.previewImage({
			urls: [url],
		})
	}
	// 调起客户端扫码界面，扫码成功后返回对应的结果。
	const iconClick = () => {
		uni.scanCode({
			scanType: ['qrCode', 'barCode'],//扫码类型
			success: async function (res) {
				code.value = res.result
				await addCode()
			},
			fail: (err) => {
				// #ifdef H5
				uni.showToast({
					icon: 'none',
					title: '设备不支持此功能',
					duration: 3000
				})
				// #endif
			}
		})
	}
	// 添加编码
	const addCode = async () => {
		if (code.value) {
			uni.showLoading({
				title: "加载中...",
			})
			await apiCode.getRegisterSecurityCheck({ code: code.value.trim() }).then(res => {
				if (res.code == 20000) {
					// 判断是否登记激活
					if (!res.data.row.register) {
						popup.value.close()
						uni.redirectTo({
							url: '/pages_user/card/acitvation?id=' + res.data.row.id
						})
					} else {
						uni.showToast({
							title: '该产品已登记激活！',
							icon: 'none'
						})
					}
				}
			})
			uni.hideLoading()
		} else {
			uni.showToast({
				title: '请先输入编码',
				icon: 'none'
			})
		}
	}
</script>
<style lang="scss" scoped>
	.box_conten {
		/* #ifdef H5 */
		height: calc(100vh - 44px);
		/* #endif */
		/* #ifndef H5 */
		height: 100vh;
		/* #endif */
		background-color: #f6f7fa;
		display: flex;
		flex-direction: column;
		overflow: hidden;

		.main_bg {
			display: flex;
			flex: 1;
			flex-direction: column;
			overflow: hidden;

			.scroll_y {
				height: 100%;

				::-webkit-scrollbar {
					width: 0;
					height: 0;
					background-color: transparent;
					display: none;
				}

				.back_mag {
					padding-bottom: 200rpx;
					box-sizing: border-box;
				}
			}

			.void_goods {
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
				width: 100%;
				height: 60vh;

				.img {
					width: 60%;
					height: 250rpx;
				}

				.tip {
					color: #aaa;
					font-size: 28rpx;
				}
			}

			.no_some {
				font-size: 24rpx;
				color: #ccc;
				text-align: center;
				width: 100%;
			}

			.item_li {
				width: calc(100% - 48rpx);
				display: flex;
				justify-content: center;
				flex-direction: column;
				align-items: center;
				background-color: #fff;
				margin: 24rpx;
				border-radius: 4rpx;
				box-shadow: 0 0 20rpx 10rpx #f0f0f0;
				position: relative;
				padding: 20rpx 0;

				.top_brand_type {
					width: 100%;
					display: flex;
					align-items: center;
					gap: 8rpx;

					.bule {
						color: #007cff;
						margin-right: 14rpx;
						line-height: 24rpx;
						height: 24rpx;
						font-size: 22rpx;
					}

					.tag {
						background-color: #007cff;
						color: #fff;
						font-size: 22rpx;
						border-radius: 4rpx;
						height: 28rpx;
						padding: 2rpx 4rpx;

						&.tag_p {
							background-color: #ccc;
							color: #fff;
						}
					}

					.day_color {
						background-color: #007cff;
						color: #fff;
						font-size: 22rpx;
						height: 28rpx;
						padding: 2rpx 4rpx;
						border-radius: 4rpx;
					}
				}

				.conent {
					display: flex;
					// align-items: center;

					width: 100%;

					.left_img {
						margin: 0 20rpx;
						width: 160rpx;
						height: 160rpx;
						background-color: #fff;
						border-radius: 4rpx;
						border: 1px solid #eee;

						.img {
							width: 100%;
							height: 100%;
							border-radius: 4rpx;
						}
					}

					.right_text {
						font-size: 24rpx;
						margin-right: 12rpx;
						flex: 1;

						.brands {
							display: -webkit-box;
							-webkit-line-clamp: 2;
							-webkit-box-orient: vertical;
							overflow: hidden;
							word-wrap: break-word;
							word-break: break-all;
							font-weight: bold;
							font-size: 28rpx;
							margin: 0rpx 0 12rpx;
						}


						.goods_ms {
							display: flex;
							justify-content: space-between;
							align-items: center;
							width: 100%;

							.ms {
								display: flex;
								justify-content: center;
								flex-direction: column;
								align-items: center;
								width: 100%;
								word-wrap: break-word;
								word-break: break-all;
								margin: 8rpx;

								.tip {
									color: #777;
								}
							}

							.line {
								height: 50rpx;
								width: 2rpx;
								background-color: #ccc;
							}
						}


						.pt {
							margin-top: 10rpx;

							display: flex;

							.tip {
								color: #777;
							}

							.lis_con {
								flex: 1;
								display: -webkit-box;
								-webkit-line-clamp: 2;
								-webkit-box-orient: vertical;
								overflow: hidden;
								word-wrap: break-word;
								word-break: break-all;
							}
						}
					}
				}
			}

		}
	}


	.add_main {
		background-color: #fff;
		border-radius: 10rpx;
		width: 80vw;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		padding: 24rpx;
		position: relative;

		.close_this {
			position: absolute;
			right: 24rpx;
			top: 24rpx;
		}

		.onlogout {
			font-size: 28rpx;
			margin: 30rpx 20rpx 0;
			height: 80rpx;
			border-radius: 50rpx;
			line-height: 80rpx;
			width: 100%;
			color: #fff;
			background-color: #007cff;
		}

		:deep(.is-input-border) {
			// border-radius: 50rpx !important;
			background-color: rgba(243, 244, 248, 1) !important;
			padding: 10rpx;
			margin-top: 20rpx;
			border: none;
			height: 80rpx !important;
			line-height: 80rpx;
			font-size: 28rpx
		}
	}

	.btn_logout {
		position: fixed;
		bottom: 0rpx;
		margin: 0 auto;
		width: 100%;
		z-index: 3;
		background-color: #fff;
		padding: 30rpx 0;
		border-top: 1px solid #eee;

		:deep(button:after) {
			border: none !important;
		}

		.onlogout {
			font-size: 28rpx;
			padding: 0rpx 20rpx;
			margin: 0 48rpx;
			height: 80rpx;
			border-radius: 50rpx;
			line-height: 80rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			background-color: #007cff;
		}
	}
</style>